package org.fastiot.data.sampling

class Timing {

    Integer packetCode
    String hexPacketCode
    String terminalCode
    Date dateAcquired

    String metadata
    String contents

    Date dateCreated

    static transients = ['hexPacketCode']

    static constraints = {
        packetCode(nullable: false)
        hexPacketCode(blank: false)
        terminalCode(blank: false)
        dateAcquired(nullable: false)
        metadata(blank: false)
        contents(blank: false)
        dateCreated()
    }

    static mapping = {
        version false
        table "timing_sampling"
        metadata type: "text"
        contents type: "text"
        dateAcquired index: "Timing_DateAcquired_TerminalCode_PacketCode_Index"
        terminalCode index: "Timing_DateAcquired_TerminalCode_PacketCode_Index"
        packetCode index: "Timing_DateAcquired_TerminalCode_PacketCode_Index"
    }

    def beforeValidate() {
        if (packetCode && !hexPacketCode)
            hexPacketCode = Integer.toHexString(packetCode)
        else if (hexPacketCode && !packetCode)
            packetCode = Integer.parseInt(hexPacketCode, 16)
    }

    def afterLoad() {
        if (packetCode)
            hexPacketCode = Integer.toHexString(packetCode)
    }
}
